//
//  MenuView.m
//  News
//
//  Created by Han on 15/4/24.
//  Copyright (c) 2015年 IPingLun. All rights reserved.
//

#import "MenuView.h"

@implementation MenuView


+ (MenuView *)sharedInstance {
    static dispatch_once_t pred;
    static MenuView *view;
    dispatch_once(&pred, ^{
        view = [[self alloc] init];
    });
    return view;
}


- (id)init {
    
    CGRect frame = CGRectMake(MAIN_SCREEN_WIDTH + MENU_WIDTH, 0, MENU_WIDTH, MAIN_SCREEN_HEIGHT);
    
    if (self = [super initWithFrame:frame]) {
      
        [self drawView];
        //[self addPanGesture];
    }
    return self;
}

- (void) addPanGesture {
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panToBack:)];
    
    panGestureRecognizer.cancelsTouchesInView = NO;
    [self addGestureRecognizer:panGestureRecognizer];
}


- (void)panToBack:(UIPanGestureRecognizer*)pan {

    
    CGPoint currentPostion = [pan translationInView:self];
    
    if (fabs(currentPostion.x) < fabs(currentPostion.y)) return;
    
    [self.menuViewDelegate panToMoveMenu:currentPostion.x];
}


- (void)drawView {
    self.backgroundColor = [UIColor whiteColor];
    
    
    /*
    NSData *gif = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"]];
    
    FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:gif];
    mFLAnimatedImageView = [[FLAnimatedImageView alloc] init];
    mFLAnimatedImageView.animatedImage = image;
    mFLAnimatedImageView.frame = CGRectMake(0, 160, 180, 135);
    [self addSubview:mFLAnimatedImageView];
    */

    UIButton *button1 = UIButton.new;
    button1.tag = 0;
    button1.backgroundColor = [UIColor greenColor];
    [button1 addTarget:self action:@selector(clickButton1:) forControlEvents:UIControlEventTouchUpInside];
    button1.selected = YES;
    [self addSubview:button1];
    
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.top).offset(50);
        make.left.equalTo(self.left).offset(0);
        make.size.equalTo(CGSizeMake(60, 28));
    }];
    
    
    
    UIButton *button2 = UIButton.new;
    button2.backgroundColor = [UIColor redColor];
    [button2 addTarget:self action:@selector(clickButton2:) forControlEvents:UIControlEventTouchUpInside];
    button2.selected = YES;
    [self addSubview:button2];
    
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.top).offset(80);
        make.left.equalTo(self.left).offset(0);
        make.size.equalTo(CGSizeMake(60, 28));
    }];
}

- (void)clickButton1:(UIButton *) button {
    /*
    if (mFLAnimatedImageView.isAnimating) {
        [mFLAnimatedImageView stopAnimating];
    } else {
        [mFLAnimatedImageView startAnimating];
    }
     */
}

- (void)clickButton2:(UIButton *) button {
    
    
    /*
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromRight;
    transition.delegate = self;
    [self.nav.view.layer addAnimation:transition forKey:nil];
    */
    
    AboutViewController *aboutView = [[AboutViewController alloc] init];
    
    [self.nav pushViewController:aboutView animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
